<?php

class Admin_LangController extends CMS_Controller_Action_Admin {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $lang = new CMS_Models_Lang();
        $this->view->langs = $lang->getAll();
    }

    public function deleteAction() {
        $this->disableView();
        $request = $this->_request->getParams();
        $lang = $request['edit'];
        $l = new CMS_Models_Lang();
        $l = $l->getLangById($lang);
        $l->delete();
        $this->_redirect("/admin/lang");
    }

    public function editlangAction() {
        $form = $this->editLangForm();
        $this->view->form = $form;

        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $lang = new CMS_Models_Lang();
                $lang = $lang->getLangById($values['common']['id_lang']);
                $lang->name = $values['common']['name'];
                $lang->prefix2 = $values['common']['prefix2'];
                $lang->enabled = $values['common']['enabled'];
                if ($values['common']['image'] == NULL) {
                    $lang->image = $values['common']['imageHidden'];
                } else {
                    $upload = $form->common->image->getTransferAdapter();
                    $info = $upload->getFileInfo();
                    if (!$upload->receive()) {
                        $messages = $upload->getMessages();
                        echo implode("\n", $messages);
                        exit;
                    }
                    $lang->image = $info['image']['name'];
                }
                $lang->save();
                $this->_redirect("/admin/lang");
            }
        }
    }

    private function editLangForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/lang/editlang")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $request = $this->_request->getParams();
        $lang = $request['edit'];
        $l = new CMS_Models_Lang();
        $l = $l->getLangById($lang);
        $common = new Zend_Form_SubForm();
        $name = $common->createElement('text', 'name');
        $name->setAttrib("class", "text-long");
        $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
        $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
        $name->setValue($l->name);

        $prefix2 = $common->createElement('text', 'prefix2');
        $prefix2->setAttrib("class", "text-short");
        $prefix2->setAttrib("size", "2");
        $prefix2->addValidator('NotEmpty')->setLabel($this->view->t->_('*Prefijo de 2 caracteres:'))->setRequired(true);
        $prefix2->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
        $prefix2->setValue($l->prefix2);

        $enabled = $common->createElement('checkbox', 'enabled');
        $enabled->setLabel('Activo');
        $enabled->setValue($l->enabled);

        $image = new Zend_Form_Element_File('image');
        $image->addValidator('Extension', false, array('gif', 'jpg', 'png'))->addErrorMessage('Formato de imagen no valido');
        $image->setDestination(str_replace("\\", "/", dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/upload/lang/');

        $imageHidden = $common->createElement('hidden', 'imageHidden');
        $imageHidden->setLabel("Imagen:");
        $imageHidden->setValue($l->image);

        $this->view->value = $l->image;
        $id = $common->createElement("hidden", 'id_lang');
        $id->setValue($l->id);
        $common->addElement($name);
        $common->addElement($prefix2);
        $common->addElement($enabled);
        $common->addElement($imageHidden);
        $common->addElement($image);
        
        $common->addElement($id);
        $form->addSubForms(array("common" => $common));
        $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
        return $form;
    }

    private function addLangForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/lang/newlang")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $common = new Zend_Form_SubForm();
        $name = $common->createElement('text', 'name');
        $name->setAttrib("class", "text-long");
        $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
        $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));

        $prefix2 = $common->createElement('text', 'prefix2');
        $prefix2->setAttrib("class", "text-short");
        $prefix2->setAttrib("size", "2");
        $prefix2->addValidator('NotEmpty')->setLabel($this->view->t->_('*Prefijo de 2 caracteres:'))->setRequired(true);
        $prefix2->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));

        $enabled = $common->createElement('checkbox', 'enabled');
        $enabled->setLabel('Activo');

        $image = new Zend_Form_Element_File('image');
        $image->setLabel('Imagen');
        $image->addValidator('Extension', false, array('gif', 'jpg', 'png'))->addErrorMessage('Formato de imagen no valido');
        $image->setDestination(str_replace("\\", "/", dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/upload/lang/');
        $common->addElement($name);
        $common->addElement($prefix2);
        $common->addElement($enabled);
        $common->addElement($image);
        $form->addSubForms(array("common" => $common));
        $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
        return $form;
    }

    public function newlangAction() {
        $form = $this->addLangForm();
        $this->view->form = $form;

        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $lang = new CMS_Models_Lang();
                $lang = $lang->createRow();
                $lang->name = $values['common']['name'];
                $lang->prefix2 = $values['common']['prefix2'];
                $lang->enabled = $values['common']['enabled'];
                if ($values['common']['image'] == NULL) {
                    $lang->image = '';
                } else {
                    $upload = $form->common->image->getTransferAdapter();
                    $info = $upload->getFileInfo();
                    if (!$upload->receive()) {
                        $messages = $upload->getMessages();
                        echo implode("\n", $messages);
                        exit;
                    }
                    $lang->image = $info['image']['name'];
                }
                $lang->save();
                $this->_redirect("/admin/lang");
            }
        }
    }

}

?>